package com.wangyuhang.Controller;

import com.wangyuhang.domain.User;
import com.wangyuhang.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*@ResponseBody
@Controller*/
@RestController  /*等同于@ResponseBody 和@Controller*/
@RequestMapping("/users")
public class userController {
    @Autowired
    private userService service;

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        User user = service.findById(id);
        int code=user!=null ?Code.SEARCH_SUCCESS:Code.SEARCH_ERR;
        String msg=user!=null?"":"查询失败";
        Result result=new Result(user,code,msg);
        return result;
    }

    @GetMapping
    public Result findAll() {
        /*int i=3/0;*/   /*测试异常*/
        List<User> list = service.findAll();
        int code=list!=null ?Code.SEARCH_SUCCESS:Code.SEARCH_ERR;
        String msg=list!=null?"":"查询失败";
        Result result=new Result(list,code,msg);
        return result;
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        boolean b = service.deleteById(id);
        return b;
    }
    @PostMapping
    public boolean save(@RequestBody User user){
        service.save(user);
        System.out.println(user);
        return true;
    }
    @PutMapping
    public boolean update(@RequestBody User user){
        service.update(user);
        return true;
    }
}
